package com.cn.ccp.action.course;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.jfree.util.Log;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.CourseContentBean;
import com.cn.ccp.bean.CourseContentManageBean;
import com.cn.ccp.bean.CourseEvaluationBean;
import com.cn.ccp.bean.CourseNoticeBean;
import com.cn.ccp.bean.CourseOutlineBean;
import com.cn.ccp.bean.CourseSettingBean;
import com.cn.ccp.bean.FilePathBean;
import com.cn.ccp.bean.ResourseBean;
import com.cn.ccp.bean.SpecialtyBean;
import com.cn.ccp.bean.TeachingGroupBean;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.Contants;

public class CourseAction extends BaseAction {
	private Logger log = Logger.getLogger(CourseAction.class);
	private List<CourseBean> courseList ;
	private SelectItem[] specialtySelectItem ;
	private CourseBean courseBean ;
	
	private CourseContentBean courseContentBean ;
	private List<CourseContentManageBean> courseChapterList ;
	private static String VIEW_MESSAGES = "messages",VIEW_NOTICES = "listNotice",VIEW_OUTLINE="outline",VIEW_EVALUA = "evaluation";
	
	private boolean disabled = false ;
	private boolean hiddenButton = false ;
	private boolean hiddenUploadFileButton = false ;
	private boolean hiddenToLookFile = false ;
	
	private SelectItem[] teachingGroupSelect ;
	private SelectItem[] courseTypeSelect ;
	private SelectItem[] courseLangeageSelect ;
	
	private CourseOutlineBean courseOutLineBean ;//大纲
	private String courseOutline;//课程大纲
	private String courseAssessment;//考核办法
	private CourseEvaluationBean assessmentBean;//考核办法
	
	private List<CourseSettingBean> trainingCourseList ;//作业显示list
	private List<CourseSettingBean> courseSurveyList ;//调查显示list
	private List<CourseSettingBean> courseTestList ;//试题显示list
	
	private List<CourseNoticeBean> noticeList;//公告列表
	
	private boolean showEditButton = true ;
	
	private boolean showSubmitApproveButton = true ;//当没有提交课程的时候是否显示   提交审核  按钮
	
	private boolean hiddenShowCourseContentFile = false ;//隐藏显示课程内容文件链接
	private boolean showEditOutline;//显示编辑输入的大纲
	private boolean showEditEvaluation;//显示可以编辑输入考核办法
	private String evaluationFileName;//考核办法文件名称
	
	private boolean initData;//初始化数据
	
	
	
	
	public boolean isInitData() {
		initData();
		return initData;
	}

	public void setInitData(boolean initData) {
		this.initData = initData;
	}

	public String editCourseContentCancel(){

		File file = new File(CCPContants.SYS_PATH+File.separator+"files"+File.separator+CCPContants.getCurrUserId()+File.separator+courseContentBean.getSource_file_url());
		if(file.exists()) file.delete();
		
		file = new File(CCPContants.SYS_PATH+File.separator+"files"+File.separator+CCPContants.getCurrUserId()+File.separator+courseContentBean.getSection_file_url());
		if(file.exists()) file.delete();
		
		return "main" ;
	}
	
	public void initData(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		if(varMap.get("zjId") != null && !varMap.get("zjId").equals("")){
			String zjId = varMap.get("zjId") ;
			log.info("---------------zj id:"+zjId);
			getCourseContentService().deleteZJ(zjId);
		}else if(varMap.get("xjId") != null && !varMap.get("xjId").equals("")){
			String xjId = varMap.get("xjId") ;
			log.info("---------------xj id:"+xjId);
			getCourseContentService().deleteXJ(xjId);
		}else{
			log.info("--------null");
		}
			
	}
	
	
	
	public void goUp(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String couseDetailId = varMap.get("chapterId");
		String type = varMap.get("type");
		log.info("------------couseDetailId = "+couseDetailId);
		log.info("------------type = "+type);
		getCourseContentService().goUp(couseDetailId,type);
		courseChapterList = null ;
		getCourseChapterList();
		
	}
	
	public void goDown(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String couseDetailId = varMap.get("chapterId");
		String type = varMap.get("type");
		log.info("------------couseDetailId = "+couseDetailId);
		log.info("------------type = "+type);
		getCourseContentService().goDown(couseDetailId,type);
		courseChapterList = null ;
		getCourseChapterList();
	}
	
	
	public String updateCourse(){
		log.info("-------------------updateCourse()------------------------");
		disabled = false ;
		hiddenButton = true ;
		hiddenUploadFileButton = false ;
		hiddenToLookFile = false ;
		resetOutlineAndEvlaSession();
		return goUpdate();
	}
	
	public void delete(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		String imagePath = varMap.get("imagePath");
		boolean flag = getCourseService().delete(id);
		if(flag){
			getpagedetail();
		}
	}	
	
	private String getOutlineFilePath(){
		FilePathBean path = getFilePathService().getFlagBean();
		String savePath = CCPContants.SYS_PATH+"files"+File.separator ;
		if(path != null){
			savePath = path.getPath() ;
		}
		return savePath;
	}
	
	private void resetOutlineAndEvlaSession(){
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.removeAttribute(CCPContants.OUTLINE_FILE_PATH);
		session.removeAttribute(CCPContants.EVALUA_FILE_PATH);
		session.removeAttribute(CCPContants.FILE_TYPE);
		showEditEvaluation = true;
		showEditOutline = true;
	}
	
	
	public String goUpdate(){
		showEditButton = true ;
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		courseBean = getCourseService().getBeanById(id);
		
		if(courseBean.getImage().equals("book-default.png")){
			
		}else{
			File sourseFile = null;
			if(courseBean.getFile_path_id() !=  -1)
				sourseFile = new File(getFilePathService().findFilePathById(courseBean.getFile_path_id()).getPath()+courseBean.getImage());
			else
				sourseFile = new File(CCPContants.SYS_PATH+"files"+File.separator + courseBean.getImage());
			
			File des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+CCPContants.getUserBean().getId() + File.separator);
			if(des.mkdir()){
				des.mkdirs();
			}
			 
			des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+CCPContants.getUserBean().getId()  + File.separator +sourseFile.getName());
			
			CCPContants.copyFile(sourseFile.getPath(), des.getPath());
			courseBean.setImage(CCPContants.getCurrUserId()+"/"+des.getName());
		}
		
		
		List<SpecialtyBean> list = getSpecialtyService().getSpecialtyByUserId();
		specialtySelectItem = new SelectItem[list.size()];
		for(int i = 0 ; i < list.size() ; i++)
			specialtySelectItem[i] = new SelectItem(""+list.get(i).getId() , list.get(i).getSpecialtyname());
		courseChapterList = null ;
		List<ResourseBean> courseTypeList  = getResourseService().pageQueryResourse(1);
		if(courseTypeList != null && courseTypeList.size() >=0){
			int len = courseTypeList.size();
			courseTypeSelect = new SelectItem[len];
			for(int i=0; i<len; i++){
				courseTypeSelect[i] = new SelectItem(""+courseTypeList.get(i).getId() , courseTypeList.get(i).getName());
			}
		}else{
			courseTypeSelect = new SelectItem[0]; 
		}
		//当对课程进行编辑的时候才能进行课程设置
		
		trainingCourseList = getCourseService().getCourseSettingList(courseBean.getId(), "2");
		courseSurveyList = getCourseService().getCourseSettingList(courseBean.getId(), "3");
		courseTestList = getCourseService().getCourseSettingList(courseBean.getId(), "1");
		log.info("------------ courseBean.getId() = "+courseBean.getId());
		courseOutLineBean = getCourseService().getOutlineByCourseId(courseBean.getId());
		if(courseOutLineBean !=null && courseOutLineBean.getOutline() != null && courseOutLineBean.getOutline().endsWith(".txt")){
			showEditOutline = true;
		}else if(courseOutLineBean !=null && courseOutLineBean.getOutline() != null){//其他文件，使用组件显示
			showEditOutline = false;
			String savePath = getOutlineFilePath();
			savePath += courseOutLineBean.getOutline();
			String outlinePath  = CCPContants.copyOutline(savePath, courseOutLineBean.getOutline(),Contants.OUTLINE_DERECT);
			log.info("------------outlinePath = "+outlinePath);
			HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			session.setAttribute(CCPContants.OUTLINE_FILE_PATH, courseOutLineBean.getOutline());
		}
		log.info(courseOutLineBean);
		assessmentBean = getCourseService().getEvaluationByCourseId(courseBean.getId());
		if(assessmentBean != null && assessmentBean.getContents() != null && assessmentBean.getContents().endsWith(".txt"))
			showEditEvaluation = true;
		else if(assessmentBean != null && assessmentBean.getContents() != null){
			showEditEvaluation = false;
			String savePath = getOutlineFilePath();
			savePath += assessmentBean.getContents();
			String evaluPath  = CCPContants.copyOutline(savePath, assessmentBean.getContents(),Contants.EVALUATION_DERECT);
			log.info("------------evaluPath = "+evaluPath);
			HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			session.setAttribute(CCPContants.EVALUA_FILE_PATH, assessmentBean.getContents());
		}
		noticeList = getCourseService().getNoticeByCourseId(courseBean.getId());
		
		if(Integer.parseInt(courseBean.getCompleted()) == 1){
			disabled = true ;
			hiddenButton = false ;
			hiddenShowCourseContentFile = true ;
		}else{
			disabled = false ;
			hiddenButton = true ;
			showSubmitApproveButton = true; 
			hiddenShowCourseContentFile = false ;
		}
		if(!courseBean.getStatus().equals("1") && !courseBean.getStatus().equals("2"))
			showSubmitApproveButton = false;
		else
			showSubmitApproveButton = true;
		
		if(courseBean.getStatus().equals("3") || courseBean.getStatus().equals("6")){
			disabled = false ;
			hiddenButton = true ;
			showSubmitApproveButton = true; 
			hiddenShowCourseContentFile = false ;
		}
		log.info("--------------------------------------showEditOutline = "+showEditOutline);
		return UPDATE ;
	}
	
	/**
	 * 删除考试
	 * @return
	 */
	public String doDeleleTest(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String settingId = varMap.get("settingId");
		log.info("----------delete test settingId = "+ settingId);
		getTrainingService().doDeleteTest(settingId);
		courseTestList = getCourseService().getCourseSettingList(courseBean.getId(), "1");
		return "";
	} 
	
	
	
	public String goAdd(){
		showSubmitApproveButton = false ;
		courseBean = new CourseBean() ;
		UserBean ubean = CCPContants.getUserBean();
		ubean = getUserService().findById(ubean.getId());
		courseBean.setCourseResourse(ubean.getFaculty());
		courseChapterList = null;
		courseAssessment = "";
		courseOutline = "";
		return ADD ;
	}
	
	public String edit(){
		if(courseBean.getCourse_name() == null  || courseBean.getCourse_name().equals("")){
    		showMsg("courseName", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getTeaching_group_id() == null  || courseBean.getTeaching_group_id().equals("-1")){
    		showMsg("specialty", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getImage() == null  || courseBean.getImage().equals("")){
//    		showMsg("image", "javax.faces.component.UIInput.REQUIRED");
//    		return "";
			//如果没有设置  ，就用系统默认图片
			courseBean.setImage("files/book-default.png");
    	}
		if(courseBean.getOnline_begin_time() != null && courseBean.getOnline_begin_time().equals(""))
			courseBean.setOnline_begin_time(null);
		
		if(courseBean.getOnline_end_time() != null && courseBean.getOnline_end_time().equals(""))
			courseBean.setOnline_end_time(null);
		
		if(courseBean.getHas_test().equals("1")){
			if(courseBean.getTotal_score().equals("0")){
				showMsg("total_score", "course_page_course_submit_error_1");
	    		return "";
			}
		}else{
			courseBean.setTotal_score("0");
		}
		if(courseBean.getId() == null || courseBean.getId().equals(""))
			courseBean.setCompleted("0");
		
		if(courseBean.getLanguage_type() == null  || courseBean.getLanguage_type().equals("-1")){
    		showMsg("languageTypeSelect", "course_page_course_submit_error_6");
    		return "";
    	}
		
		if(courseBean.getCourseResourse() == null  || courseBean.getCourseResourse().equals("")){
    		showMsg("courseResourse", "course_page_course_submit_error_7");
    		return "";
    	}
		
		courseBean.setDescripte(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("content1").toString());
		boolean flag = false ;
		initFilepath();
		if(courseBean.getId() != null  && !courseBean.getId().equals("")){
			flag = getCourseService().update(courseBean );
		}else{
			courseBean.setStatus("1");
			int couresId = getCourseService().sava(courseBean);
			flag = couresId > 0 ;
			courseBean.setId(""+couresId);
		}
		if(!flag)
			return "";
		courseChapterList = null ;
		return MAIN;
	}
	
	 private void initFilepath(){
		 FilePathBean pathBean = getFilePathService().getFlagBean();
			String imgFile = pathBean.getPath()+courseBean.getImage();
			File file = new File(imgFile);
			if(!file.exists() ){
				if(courseBean.getId() != null  && !courseBean.getId().equals("")){
					CourseBean tub = getCourseService().getBeanById(courseBean.getId());
					courseBean.setImage(tub.getImage());
					courseBean.setFile_path_id(tub.getFile_path_id());
				}
			}else{
				courseBean.setFile_path_id(pathBean.getId());
			}
	 }
	
	
	public String editToCourseContent(){
		if(courseBean.getCourse_name() == null  || courseBean.getCourse_name().equals("")){
    		showMsg("courseName", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getTeaching_group_id() == null  || courseBean.getTeaching_group_id().equals("-1")){
    		showMsg("specialty", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getImage() == null  || courseBean.getImage().equals("")){
    		showMsg("image", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getOnline_begin_time() != null && courseBean.getOnline_begin_time().equals(""))
			courseBean.setOnline_begin_time(null);
		
		if(courseBean.getOnline_end_time() != null && courseBean.getOnline_end_time().equals(""))
			courseBean.setOnline_end_time(null);
		
		
		if(courseBean.getHas_test().equals("1")){
			if(courseBean.getTotal_score().equals("0")){
				showMsg("total_score", "course_page_course_submit_error_1");
	    		return "";
			}
		}else{
			courseBean.setTotal_score("0");
		}
		boolean flag = false ;
		initFilepath();
		if(courseBean.getId() != null  && !courseBean.getId().equals("")){
			flag = getCourseService().update(courseBean );
		}else{
			courseBean.setStatus("1");
			int couresId = getCourseService().sava(courseBean);
			flag = couresId > 0 ;
			courseBean.setId(""+couresId);
		}
		if(!flag)
			return "";
		courseChapterList = null ;
		courseContentBean = new CourseContentBean();
		return "editCourseContent" ;
	}
	
	public String goAddCourseContent(){//方便在添加课程章节内容的时候直接写入数据库。前提是课程必须已经创建
		if(courseBean.getCourse_name() == null  || courseBean.getCourse_name().equals("")){
    		showMsg("courseName", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getTeaching_group_id() == null  || courseBean.getTeaching_group_id().equals("-1")){
    		showMsg("specialty", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getImage() == null  || courseBean.getImage().equals("")){
    		showMsg("image", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getOnline_begin_time() != null && courseBean.getOnline_begin_time().equals(""))
			courseBean.setOnline_begin_time(null);
		
		if(courseBean.getOnline_end_time() != null && courseBean.getOnline_end_time().equals(""))
			courseBean.setOnline_end_time(null);
		courseContentBean = new CourseContentBean();
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute("multi", true);
		session.setAttribute("limit", 10);
		return "editCourseContent" ;
	}
	
	
	public void numberOnkeyUpEvent(){
		if(courseContentBean.getChapter_number().equals("-1")){
			courseContentBean.setChapter_title_states(true);
			courseContentBean.setChapter_title("");
			courseContentBean.setSection_index("-1");
			courseContentBean.setSection_title_states(true);
			return ;
		}
		
		
		log.info("------------"+courseBean.getId());
		log.info("------------: " + courseContentBean.getChapter_number());
		courseContentBean.setSection_number("1");
		courseContentBean.setChapter_title("");
		courseContentBean.setSection_index("-1");
		courseContentBean.setSection_title("");
		log.info("courseChapterList = "+courseChapterList);
		List<CourseContentBean> list = getCourseContentService().getChapterDetailList(courseBean.getId(), courseContentBean.getChapter_number());
		courseContentBean.setChapter_title_states(false);
		if(list.size() > 0){
			courseContentBean.setChapter_title(list.get(0).getChapter_title());
			courseContentBean.setChapter_title_states(true);
		}
		courseContentBean.setSection_number(String.valueOf((list.size()+1)));
		log.info("------------courseContentBean.getChapter_number() = "+courseContentBean.getChapter_number());
		if("1".equalsIgnoreCase(courseContentBean.getChapter_number()))
			courseContentBean.setCan_browse("1");
		else
			courseContentBean.setCan_browse("0");
		log.info(courseContentBean);
	}
	
	
	public void xiaojieNumberOnkeyUpEvent(){
		courseContentBean.setSection_title("");
		if(courseContentBean.getChapter_number().equals("-1")){
			courseContentBean.setSection_index("-1");
			return ;
		}
		
		if(courseContentBean.getSection_index().equals("-1")){
			courseContentBean.setSection_title_states(true);
			return ;
		}
		
		
		courseContentBean.setCourse_id(courseBean.getId());
		log.info("courseChapterList = "+courseChapterList);
		List<String> list = getCourseContentService().getXiaojieTitleList(courseContentBean);
		courseContentBean.setSection_title_states(false);
		log.info(courseContentBean.getSection_index());
		for(String a : list)
			log.info(a);
		if(list.size() > 0 && list.size() >= Integer.parseInt(courseContentBean.getSection_index())+1){
			courseContentBean.setSection_title(list.get(Integer.parseInt(courseContentBean.getSection_index()) ));
			courseContentBean.setSection_title_states(true);
		}
		courseContentBean.setSection_number(String.valueOf((list.size()+1)));
		if("1".equalsIgnoreCase(courseContentBean.getChapter_number()))
			courseContentBean.setCan_browse("1");
		else
			courseContentBean.setCan_browse("0");
		log.info(courseContentBean);
	}
	
	
	
	
	
	
	
	public String editCourseContent(){
		
		String zhangjieTitle = "";
		boolean fl = getCourseContentService().checkHaveZJ(courseBean.getId(), courseContentBean.getChapter_number());
		if(!fl){//没有章节信息，先创建创建章节信息
			CourseContentBean tccb = new CourseContentBean();
			tccb.setCourse_id(courseBean.getId());
			tccb.setChapter_number(courseContentBean.getChapter_number());
			tccb.setChapter_title(courseContentBean.getChapter_title());
			tccb.setSection_number("0");
			tccb.setCan_browse("1");
			tccb.setFilePathId(courseContentBean.getFilePathId());
			tccb.setStatus("0");
			
			getCourseContentService().sava(tccb);
			CourseContentBean zjBean = getCourseContentService().showBeanByTitile(courseBean.getId(), courseContentBean.getChapter_title());
			courseContentBean.setChapter_number(zjBean.getChapter_number());
		}
		
		
		if(courseContentBean.getChapter_number() == null || courseContentBean.getChapter_number().equals("")){
			showMsg("chapter_number", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(courseContentBean.getChapter_title() == null || courseContentBean.getChapter_title().equals("")){
			showMsg("chapter_title", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(courseContentBean.getSection_number() == null || courseContentBean.getSection_number().equals("")){
			showMsg("section_number", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		
		
		
		if(!courseContentBean.getSection_title().equals("") ||
				!courseContentBean.getSection_index().equals("-1")){
			
			if(courseContentBean.getSection_index() == null || courseContentBean.getSection_index().equals("-1")){
				showMsg("chapter_number2", "javax.faces.component.UIInput.REQUIRED");
				return "";
			}
			if(courseContentBean.getSection_title() == null || courseContentBean.getSection_title().equals("")){
				showMsg("chapter_title2", "javax.faces.component.UIInput.REQUIRED");
				return "";
			}
			
			if(courseContentBean.getSection_file_url().trim().equals("")){
				showMsg("image2", "javax.faces.component.UIInput.REQUIRED");
				return "";
			}
			courseContentBean.setStatus(courseContentBean.getChapter_number());
		}
		
		//int id ,String courseid ,String chapter_number ,String section_number
		//针对多个文件进行处理
		
		
		zhangjieTitle =  courseContentBean.getChapter_title();
		if(!courseContentBean.getSection_title().equals("")){
			courseContentBean.setChapter_title(courseContentBean.getSection_title());
		}
		
		log.info(courseContentBean.getSource_file_url());
		log.info(courseContentBean.getSection_file_url());
		
		String[] uploadSourseFileName = courseContentBean.getSource_file_url().split(";") ;
		String[] uplaodFileName = courseContentBean.getSection_file_url().trim().split(";");
		Map<String,String> fileMap = new HashMap<String,String>();
		List<String> list = new ArrayList<String>(); 
		for(int i=0,n=uploadSourseFileName.length; i<n;i++){
			if(uploadSourseFileName[i].equals(""))
				continue;
			fileMap.put(uploadSourseFileName[i], uplaodFileName[i]);
			list.add(uploadSourseFileName[i]);
		} 
		uploadSourseFileName = new String[list.size()];
		list.toArray(uploadSourseFileName);
		Arrays.sort(uploadSourseFileName, 0, list.size());
		
		boolean flag = false; 
		courseContentBean.setCourse_id(courseBean.getId());
		boolean isHaveXJ = false ;
		for (int i = 0,n = uploadSourseFileName.length; i < n; i++) {
			if(uploadSourseFileName[i].equals(""))
				continue;
			log.info(uploadSourseFileName[i]);
			courseContentBean.setSource_file_url(uploadSourseFileName[i]);
			if(uploadSourseFileName.length != 1)
			courseContentBean.setSection_file_name(uploadSourseFileName[i].split("\\.")[0]);
				
			courseContentBean.setSection_file_type(uploadSourseFileName[i].split("\\.")[1]);
			courseContentBean.setSection_file_url(fileMap.get(uploadSourseFileName[i]));
			
			//TODO 2014-04-01暂时不对序号进行检测
			boolean sNumberFlag = false ;//getCourseContentService().checkSectionNumber(courseContentBean.getId() ,""+courseBean.getId() ,courseContentBean.getChapter_number(), courseContentBean.getSection_number() );
			if(sNumberFlag){//该序号已经存在
				showMsg("section_number", "course_content_page_section_number_is_allready");
				courseContentBean.setSection_number("");
				return "";
			}
			if(courseContentBean.getSection_file_name() == null || courseContentBean.getSection_file_name().equals("")){
				showMsg("section_file_name", "javax.faces.component.UIInput.REQUIRED");
				return "";
			}
			if(courseContentBean.getSource_file_url() == null || courseContentBean.getSource_file_url().equals("")){
				showMsg("source_file_url", "javax.faces.component.UIInput.REQUIRED");
				return "";
			}
			if(courseContentBean.getHas_training() == null || courseContentBean.getHas_training().equals("")){
				showMsg("has_training", "javax.faces.component.UIInput.REQUIRED");
				return "";
			}
			if(courseContentBean.getSection_file_type() == null || courseContentBean.getSection_file_type().equals("")){
				String filename = courseContentBean.getSection_file_name();
				String[] strs = filename.split("\\.") ;
				String type = strs[strs.length-1];
				courseContentBean.setSection_file_type(type);
			}
			
			if(courseContentBean.getHas_training().equals("1")){
				if(courseContentBean.getTotal_score() == 0){
					showMsg("total_score", "javax.faces.component.UIInput.REQUIRED");
					return "";
				}
			}else{
				courseContentBean.setTotal_score(0);
			}
			
			courseContentBean.setSection_file_url(CCPContants.getCurrUserId()+"/"+courseContentBean.getSection_file_url());
			
			courseContentBean.setCourse_id(courseBean.getId());
		
			
			if(courseContentBean.getId() == 0){
				if(courseContentBean.getSection_index().equals("-1")){//直接添加到章节下
					int sectionNumber = getCourseContentService().getCurrNumber(courseContentBean);
					sectionNumber += 1;//将小节编号加1
					courseContentBean.setSection_number(String.valueOf(sectionNumber));
				}else if(courseContentBean.getId() == 0){//存在小节信息，将内容添加到小节下
					List<String> sectionNumberList = getCourseContentService().getXiaoJieSectionNumber(courseContentBean);
//					log.info("--------------- sectionNumberList.size() = "+sectionNumberList.size());
					if(sectionNumberList == null || sectionNumberList.size() == 0){//没有小节信息
						int sectionNumber = getCourseContentService().getCurrNumber(courseContentBean); 
						sectionNumber += 1;//将小节编号加1
						courseContentBean.setSection_number(String.valueOf(sectionNumber));
						courseContentBean.setOther(String.valueOf("1"));//other 字段昨晚三级目录序号
					}else{
						courseContentBean.setSection_number(String.valueOf(sectionNumberList.get(0)));
						int otherNumber = sectionNumberList.size() + 1;
						courseContentBean.setOther(String.valueOf(otherNumber));//other 字段昨晚三级目录序号
					}
					
				}
				flag = getCourseContentService().sava(courseContentBean);
			}else{
				String imgFile = getFilePathService().getFlagBean().getPath()+courseContentBean.getSection_file_url();
				File file = new File(imgFile);
				if(!file.exists()){
					CourseContentBean tub = getCourseContentService().showBean(""+courseContentBean.getId());
					courseContentBean.setSection_file_url(tub.getSection_file_url());
				}
				getCourseContentService().updateZhangeJieTitle(""+courseContentBean.getId(), zhangjieTitle ,courseContentBean.getChapter_title());
				flag = getCourseContentService().update(courseContentBean);
			}
		}
		
		courseChapterList = null ;
		return MAIN ;
	}
	
	public void deleteCourseContent(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String courseDetailId = varMap.get("chapterId");
		String stype = varMap.get("type");
		log.info("----------------------courseDetailId = "+courseDetailId);
		int type = 1;
		try {
			type = Integer.valueOf(stype);
		}catch(Exception e){}
		log.info("-----------------------type = "+type);
		getCourseContentService().delete(courseDetailId,type);

	}
	//提交课程审核
	/**
	 * @return
	 */
	public String submitCourseApprove(){
		if(courseBean.getCourse_name() == null  || courseBean.getCourse_name().equals("")){
    		showMsg("courseName", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getTeaching_group_id() == null  || courseBean.getTeaching_group_id().equals("-1")){
    		showMsg("specialty", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getImage() == null  || courseBean.getImage().equals("")){
    		showMsg("image", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
		if(courseBean.getLanguage_type() == null  || courseBean.getLanguage_type().equals("-1")){
    		showMsg("languageTypeSelect", "course_page_course_submit_error_6");
    		return "";
    	}
		if(courseBean.getCourseResourse() == null  || courseBean.getCourseResourse().equals("")){
    		showMsg("courseResourse", "course_page_course_submit_error_7");
    		return "";
    	}
		if(courseBean.getOnline_begin_time() != null && courseBean.getOnline_begin_time().equals(""))
			courseBean.setOnline_begin_time(null);
		
		if(courseBean.getOnline_end_time() != null && courseBean.getOnline_end_time().equals(""))
			courseBean.setOnline_end_time(null);
		boolean flag = false ;
		if(courseBean.getId() != null  && !courseBean.getId().equals("")){
			flag = getCourseService().update(courseBean );
		}else{
			courseBean.setStatus("1");
			int couresId = getCourseService().sava(courseBean);
			flag = couresId > 0 ;
			courseBean.setId(""+couresId);
		}
		if(!flag)
			return "";
		
		//判断是否有章节，无章节则不允许进行提交
		List<CourseContentBean> ccList = getCourseContentService().getCourseContentList(courseBean.getId());
		if(ccList.size() == 0 ){
			showMsg("hiddeninfo", "course_page_course_submit_error_5");
			return "";
		}
		
		
		
		
		//判断课程试题是否完整，课程成绩=试题总成绩
		CourseSettingBean testBean = null;
		
		try{
			testBean = getCourseService().getCourseSettingBeanForTestByCourseId(courseBean.getId());
		}catch(Exception e){
			log.error("query test failed : ", e);
		}
		
		if(testBean != null){
			int total_training_scorse = getCourseService().getTotalScore(testBean.getId(),"1");
			int total_score = Integer.parseInt(testBean.getScore());
			log.info("---total_training_scorse = "+total_training_scorse);
			log.info("---total_score = "+total_score);
			
			if(total_training_scorse < total_score ){//数量不够
				showMsg("hiddeninfo", "course_page_course_submit_error_2");
				return "";
			}else if(total_training_scorse > total_score){//数量过多
				showMsg("hiddeninfo", "course_page_course_submit_error_3");
				return "";
			}
		}
		courseChapterList = null ;
		UserBean userBean = getUserService().findById(CCPContants.getUserBean().getId());
		String courseApproveStatus = "2" ;
		if(userBean.getStatus() == 1){
			//可以直接发布课程的
			courseApproveStatus = "4" ;
		}
		getCourseService().updateCourseStatus(courseBean.getId(), courseApproveStatus);
		return MAIN ;
	}
	
	
	
	
	public String showCourseContentFile(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String chapterId = varMap.get("chapterId");
		CourseContentBean courseContentBean = getCourseContentService().showBean(chapterId);
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute(CCPContants.SWF_FILE_NAME, courseContentBean.getSection_file_url());
		String path = CCPContants.getFilePath(courseContentBean.getSection_file_url() , getFilePathService(),courseContentBean.getFilePathId(),CCPContants.getUserBean().getId());
		log.info("---------path = "+path);
//		session.setAttribute(CCPContants.SWF_FILE_NAME, path);
		session.setAttribute(CCPContants.VIDEO_FILE_NAME, courseContentBean.getSection_file_url());
		int index =  courseContentBean.getSection_file_url().lastIndexOf(".");
		String fileType = courseContentBean.getSection_file_url().substring(index+1,courseContentBean.getSection_file_url().length());
		session.setAttribute(CCPContants.FILE_TYPE, fileType);
		if(fileType.equalsIgnoreCase("swf") && courseContentBean.getSection_file_type().equalsIgnoreCase("swf"))
			return DEFAULT_SWF_URL;
		if(fileType.equalsIgnoreCase("swf") || fileType.equalsIgnoreCase("pdf"))
			return SWF_FILE;
		else{
			if("flv".equalsIgnoreCase(fileType))
				return FLV_URL;
			if(CCPContants.DEFAULT_VEDIO_FILE_TYPE.indexOf(fileType) >= 0){
				if("ogv".equalsIgnoreCase(fileType))
					fileType = "ogg";
				session.setAttribute(CCPContants.VIDEO_FILE_TYPE, fileType);
				log.info("---------default vedio file-------");
				return DEFAULT_VEDIO_FILE;
			}else
				return VEDIO_FILE;
		}
	}
	
	/**
	 * 查看学生留言
	 * @return
	 */
	public String viewMessages(){
		return VIEW_MESSAGES;
	}
	
	/**
	 * 进入公告添加和查看页面
	 * @return
	 */
	public String viewNotices(){
		return VIEW_NOTICES;
	}
	
	public String viewOutline(){
		return VIEW_OUTLINE;
	}
	
	public String viewEvalu(){
		return VIEW_EVALUA;
	}
	
	
	
	//跳转到课程内容页面
	public String goUpdateCourseContent(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String chapterId = varMap.get("chapterId");
		if(chapterId == null || chapterId.equals(""))
			return "";
		courseContentBean = getCourseContentService().showBean(chapterId);
		courseContentBean.setChapter_title_states(false);
		courseContentBean.setSection_title_states(false);
		if(courseContentBean.getStatus().equals("0")){
			courseContentBean.setSection_title_states(true);
			courseContentBean.setSection_title("");
			courseContentBean.setSection_index("-1");
		}
		
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute("multi", false);
		session.setAttribute("limit", 1);
		
		return "editCourseContent" ;
	}
	
	/**
	 * 修改标题名称
	 * @return
	 */
	public String changeSectionFileName(){
		log.info(courseContentBean);
		getCourseContentService().changeSectionFileName(courseContentBean.getSection_file_name(),courseContentBean.getId());
		return MAIN;
	}
	 
	public String chapterTitleChange(){
		log.info(courseContentBean);
		getCourseContentService().changeChapterTitleName(courseContentBean.getChapter_title(), courseContentBean.getId());
		return MAIN;
	}
	
	public void sectionNumberChange(){
		String reg ="\\d+\\.{0,1}\\d*";
		boolean isDigits = courseContentBean.getSection_number().matches(reg);
		if(!isDigits){
			courseContentBean.setSection_number("");
		}
	}
	
	
	@Override
	public void getpagedetail() {
		setItemCount(getCourseService().getCount());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		courseList = getCourseService().pageQuery(start, pagesize);
	}

	public List<CourseBean> getCourseList() {
		getpagedetail();
		disabled = false ;
		hiddenButton = true ;
		hiddenUploadFileButton = false ;
		return courseList;
	}

	public void setCourseList(List<CourseBean> courseList) {
		this.courseList = courseList;
	}

	public CourseBean getCourseBean() {
		//if(courseBean != null){
		//	if(courseBean.getId() != null && courseBean.getFile_path_id() != 0)
		//		CCPContants.getFilePath(courseBean.getImage() , getFilePathService(),courseBean.getFile_path_id() , CCPContants.getUserBean().getId());
		//}
		if(courseBean == null)
			courseBean = new CourseBean();
		Log.info(courseBean.getImage());
		return courseBean;
	}

	public void setCourseBean(CourseBean courseBean) {
		this.courseBean = courseBean;
	}

	public SelectItem[] getSpecialtySelectItem() {
		List<SpecialtyBean> list = getSpecialtyService().getSpecialtyByUserId();
		specialtySelectItem = new SelectItem[list.size()];
		for(int i = 0 ; i < list.size() ; i++)
			specialtySelectItem[i] = new SelectItem(""+list.get(i).getId() , list.get(i).getSpecialtyname());
		if(specialtySelectItem == null)
			specialtySelectItem = new SelectItem[0];
		return specialtySelectItem;
	}

	public void setSpecialtySelectItem(SelectItem[] specialtySelectItem) {
		this.specialtySelectItem = specialtySelectItem;
	}

	public CourseContentBean getCourseContentBean() {
		@SuppressWarnings("unchecked")
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String fileName = varMap.get("fileName");
		String swfPath = varMap.get("swfPath");
		String sourseFileName = varMap.get("sourseFileName");
		String errorInfo = varMap.get("errorInfo");
		if(courseContentBean == null)
			courseContentBean = new CourseContentBean();
		if(fileName != null && !fileName.equals("")){
			courseContentBean.setSource_file_url("files/"+CCPContants.getCurrUserId()+"/"+fileName);
			//考虑中文文件的问题，传递过来的为乱码
			courseContentBean.setSection_file_name(sourseFileName);
			courseContentBean.setSection_file_type(fileName.split("\\.")[1]);
			courseContentBean.setSection_file_url(swfPath);
		}
		if(errorInfo != null && !errorInfo.equals("")){
			showMsg("section_file_name", errorInfo);
			courseContentBean.setSource_file_url("");
			//考虑中文文件的问题，传递过来的为乱码
			courseContentBean.setSection_file_name("");
			courseContentBean.setSection_file_type("");
			courseContentBean.setSection_file_url("");
		}
		return courseContentBean;
	}

	public void setCourseContentBean(CourseContentBean courseContentBean) {
		this.courseContentBean = courseContentBean;
	}

	public List<CourseContentManageBean> getCourseChapterList() {
		log.info("--------------------courseBean.getId() = "+courseBean.getId());
		List<CourseContentBean> zList = getCourseContentService().getCourseContentList(courseBean.getId());
		courseChapterList = new ArrayList<CourseContentManageBean>();
		CourseContentManageBean firstLevelBean = null;//章节bean
		CourseContentManageBean secondLevelBean = null;//小节节bean
		String secondTitle = "";
		for(int i=0 , n=zList.size(); i<n; i++){
			CourseContentBean b = zList.get(i);
			log.info(b);
			if(firstLevelBean == null){
				firstLevelBean =  addFirstLevel(b,courseChapterList,true);
			}else{
				if(b.getChapter_number().equalsIgnoreCase(firstLevelBean.getChapter_number())){//一个章节的内容
					if("0".equalsIgnoreCase(b.getStatus())){//章节下面的课程内容
						firstLevelBean.getCcList().add(b);
					}else{
						if(secondLevelBean == null){//小节bean为空
							secondLevelBean = addSecondLevel(b,true);
							firstLevelBean.getXjMList().add(secondLevelBean);
							secondTitle = b.getChapter_title();
							continue;
						}
						
						if(secondTitle.equals(b.getChapter_title())){//同一小节的课程内容
							secondLevelBean.getCcmList().add(b);
						}else{//不是一个小节的内容，则创建新的小节
							secondLevelBean = addSecondLevel(b,true);
							firstLevelBean.getXjMList().add(secondLevelBean);
							secondTitle = b.getChapter_title();
						}
						
					}
					
				}else{//不是一个章节的内容，添加一个新的章节目录
					firstLevelBean =  addFirstLevel(b,courseChapterList,true);
					secondLevelBean = null;
				}
				
			}
		}
		
		return courseChapterList;
	}

	
	public void setCourseChapterList(List<CourseContentManageBean> courseChapterList) {
		this.courseChapterList = courseChapterList;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}
	public boolean isHiddenButton() {
		return hiddenButton;
	}

	public void setHiddenButton(boolean hiddenButton) {
		this.hiddenButton = hiddenButton;
	}

	public boolean isHiddenUploadFileButton() {
		return hiddenUploadFileButton;
	}

	public void setHiddenUploadFileButton(boolean hiddenUploadFileButton) {
		this.hiddenUploadFileButton = hiddenUploadFileButton;
	}

	public SelectItem[] getTeachingGroupSelect() {
		UserBean bean = getUserService().findById(CCPContants.getUserBean().getId());
		List<TeachingGroupBean> teList = getTeachingGroupService().getTeachingGroupListByUId(""+bean.getId());
		if(teList.size() == 0)
			teList = getTeachingGroupService().getTeachingGroupListByFId(bean.getFacultyId());
		if(teList != null){
			teachingGroupSelect = new SelectItem[teList.size()];
			for(int i=0; i<teList.size();i++){
				teachingGroupSelect[i] = new SelectItem(""+teList.get(i).getId() , teList.get(i).getTeaching_group_name());
			}
		}
		return teachingGroupSelect;
	}

	public void setTeachingGroupSelect(SelectItem[] teachingGroupSelect) {
		this.teachingGroupSelect = teachingGroupSelect;
	}

	public boolean isHiddenToLookFile() {
		return hiddenToLookFile;
	}

	public void setHiddenToLookFile(boolean hiddenToLookFile) {
		this.hiddenToLookFile = hiddenToLookFile;
	}


	public SelectItem[] getCourseTypeSelect() {
		List<ResourseBean> courseTypeList  = getResourseService().pageQueryResourse(1);
		if(courseTypeList != null && courseTypeList.size() >=0){
			int len = courseTypeList.size();
			courseTypeSelect = new SelectItem[len];
			for(int i=0; i<len; i++){
				ResourseBean b = courseTypeList.get(i);
				courseTypeSelect[i] = new SelectItem(""+b.getId() , b.getName());
			}
		}else{
			courseTypeSelect = new SelectItem[0]; 
		}
		return courseTypeSelect;
	}

	public void setCourseTypeSelect(SelectItem[] courseTypeSelect) {
		this.courseTypeSelect = courseTypeSelect;
	}

	public CourseOutlineBean getCourseOutLineBean() {
		if(getCourseBean().getId() != null  && courseBean.getId() != null && !getCourseBean().getId().equals(""))
			courseOutLineBean = getCourseService().getOutlineByCourseId(courseBean.getId());
		if(courseOutLineBean == null)
			courseOutLineBean = new CourseOutlineBean();
		return courseOutLineBean;
	}

	public void setCourseOutLineBean(CourseOutlineBean courseOutLineBean) {
		this.courseOutLineBean = courseOutLineBean;
	}

	public CourseEvaluationBean getAssessmentBean() {
		if(getCourseBean().getId() != null  && !getCourseBean().getId().equals(""))
			assessmentBean = getCourseService().getEvaluationByCourseId(courseBean.getId());
		if(assessmentBean == null)
			assessmentBean = new CourseEvaluationBean();
		return assessmentBean;
	}

	public void setAssessmentBean(CourseEvaluationBean assessmentBean) {
		this.assessmentBean = assessmentBean;
	}

	public boolean isShowEditButton() {
		return showEditButton;
	}

	public void setShowEditButton(boolean showEditButton) {
		this.showEditButton = showEditButton;
	}

	public List<CourseSettingBean> getTrainingCourseList() {
		if(getCourseBean().getId() != null  && !getCourseBean().getId().equals(""))
			trainingCourseList = getCourseService().getCourseSettingList(courseBean.getId(), "2");	
		if(trainingCourseList == null)
			trainingCourseList = new ArrayList<CourseSettingBean>();
		return trainingCourseList;
	}

	public void setTrainingCourseList(List<CourseSettingBean> trainingCourseList) {
		this.trainingCourseList = trainingCourseList;
	}

	public List<CourseSettingBean> getCourseSurveyList() {
		if(getCourseBean().getId() != null  && !getCourseBean().getId().equals(""))
			courseSurveyList = getCourseService().getCourseSettingList(courseBean.getId(), "3");
		if(courseSurveyList == null)
			courseSurveyList = new ArrayList<CourseSettingBean>();
		return courseSurveyList;
	}

	public void setCourseSurveyList(List<CourseSettingBean> courseSurveyList) {
		this.courseSurveyList = courseSurveyList;
	}

	public List<CourseNoticeBean> getNoticeList() {
		if(getCourseBean().getId() != null  && !getCourseBean().getId().equals(""))
			noticeList = getCourseService().getNoticeByCourseId(courseBean.getId());
		if(noticeList == null)
			noticeList = new ArrayList<CourseNoticeBean>();
		return noticeList;
	}

	public void setNoticeList(List<CourseNoticeBean> noticeList) {
		this.noticeList = noticeList;
	}

	public List<CourseSettingBean> getCourseTestList() {
		if(getCourseBean().getId() != null  && !getCourseBean().getId().equals(""))
			courseTestList = getCourseService().getCourseSettingList(courseBean.getId(), "1");
		if(courseTestList == null)
			courseTestList = new ArrayList<CourseSettingBean>();
		return courseTestList;
	}

	public void setCourseTestList(List<CourseSettingBean> courseTestList) {
		this.courseTestList = courseTestList;
	}

	public SelectItem[] getCourseLangeageSelect() {
		List<ResourseBean> courseTypeList  = getResourseService().pageQueryResourse(2);
		if(courseTypeList != null && courseTypeList.size() >=0){
			int len = courseTypeList.size();
			courseLangeageSelect = new SelectItem[len];
			for(int i=0; i<len; i++){
				ResourseBean b = courseTypeList.get(i);
				courseLangeageSelect[i] = new SelectItem(""+b.getId() , b.getName());
			}
		}else{
			courseLangeageSelect = new SelectItem[0]; 
		}
		return courseLangeageSelect;
	}

	public void setCourseLangeageSelect(SelectItem[] courseLangeageSelect) {
		this.courseLangeageSelect = courseLangeageSelect;
	}

	public boolean isShowSubmitApproveButton() {
		return showSubmitApproveButton;
	}

	public void setShowSubmitApproveButton(boolean showSubmitApproveButton) {
		this.showSubmitApproveButton = showSubmitApproveButton;
	}

	public boolean isHiddenShowCourseContentFile() {
		return hiddenShowCourseContentFile;
	}

	public void setHiddenShowCourseContentFile(boolean hiddenShowCourseContentFile) {
		this.hiddenShowCourseContentFile = hiddenShowCourseContentFile;
	}

	public String getCourseOutline() {
		courseOutline = null; 
		courseOutLineBean = getCourseService().getOutlineByCourseId(courseBean.getId());
		showEditOutline = false;
		if(courseOutLineBean != null){
			if(courseOutLineBean.getOutline().toUpperCase().endsWith(".TXT")){
				String savePath = getOutlineFilePath();
				savePath += courseOutLineBean.getOutline();
				log.info(savePath);
				showEditOutline = true;
				BufferedReader reader = null;
				StringBuffer buf = new StringBuffer();
				try {
					reader = new BufferedReader(new FileReader(savePath));
					String text = null;
					while ((text = reader.readLine()) != null){
						 buf.append(text);
						 buf.append(System.getProperty("line.separator"));
					}
					courseOutline = buf.toString();
				} catch (IOException e) {
					log.info("read outline failed : ",e);
				} finally{
					try {
						if(reader != null)
							reader.close();
					} catch (IOException e) {
					}
				} 
			}//else{//其他文件，使用组件显示
//				String outlinePath  = CCPContants.copyOutline(savePath, courseOutLineBean.getOutline());
//				HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
//				session.setAttribute(CCPContants.OUTLINE_FILE_PATH, outlinePath);
//			}
		}
		log.info("-----------showEditOutline = "+showEditOutline);
		return courseOutline;
	}

	public void setCourseOutline(String courseOutline) {
		this.courseOutline = courseOutline;
	}

	public String goEditOutline(){
		showEditOutline = true;
		return "outline";
	}
	
	public String goUploadOutline(){
		showEditOutline = false;
		return "uploadoutline";
	}
	
	
	public String goEditEvaluation(){
		showEditEvaluation = true;
		return "evaluation";
	}
	
	public String goUploadEvaluation(){
		showEditEvaluation = false;
		return "uploadevaluation";
	}
	
	
	/**
	 * 上传考核办法
	 * @return
	 */
	public String uploadEvaluation(){
		log.info("-----------uploadEvaluation()-------- ");
		log.info("-------------------evaluationFileName =  "+evaluationFileName);
		if(assessmentBean == null)
			assessmentBean = new  CourseEvaluationBean();
		
		String savePath = CCPContants.SYS_PATH+"files/"+ CCPContants.getCurrUserId() + "/" + courseBean.getId() + "/" + Contants.EVALUATION_DERECT + "/";
		FilePathBean filePathBean =  getFilePathService().getFlagBean();
		if(filePathBean != null){
			savePath = filePathBean.getPath()+CCPContants.getCurrUserId() +"/" + courseBean.getId() + "/" + Contants.EVALUATION_DERECT + "/";;
		} 
		log.info("-------------------savePath =  "+savePath);
		File[] f1 = new File(savePath).listFiles();
		if(f1 == null || f1.length <= 0){
			showMsg("uploadStudent", "user_page_label_upload_student_null_error");
			return "";
		}
		for(File f:f1) {
			try {
				log.info("------"+f.getCanonicalPath());
				if(!f.getName().equalsIgnoreCase(evaluationFileName))
					f.delete();
			} catch (IOException e) {
				log.error("delete failed ", e);
			}
		}
		assessmentBean.setContents(CCPContants.getCurrUserId() + "/" + courseBean.getId() + "/" + Contants.EVALUATION_DERECT + "/" + evaluationFileName);
		if(assessmentBean != null && assessmentBean.getId() != 0)
			getCourseService().updateCourseEvaluation(assessmentBean.getId(), assessmentBean.getContents(), filePathBean.getId());
		else
			getCourseService().saveCourseEvaluation(courseBean.getId(), filePathBean.getId(), assessmentBean.getContents());
		
		String evaluPath  = CCPContants.copyOutline(savePath+evaluationFileName, assessmentBean.getContents(),Contants.EVALUATION_DERECT);
		log.info("------------evaluPath = "+evaluPath);
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute(CCPContants.EVALUA_FILE_PATH, assessmentBean.getContents());
		showEditEvaluation = false;
		return MAIN;
	}
	
	
	
	public String getCourseAssessment() {
		assessmentBean = getCourseService().getEvaluationByCourseId(courseBean.getId());
		if(assessmentBean != null){
			if(assessmentBean.getContents().toUpperCase().endsWith(".TXT")){
				String savePath = getOutlineFilePath();
				savePath += assessmentBean.getContents();
				log.info(savePath);
			
				BufferedReader reader = null;
				StringBuffer buf = new StringBuffer();
				try {
					reader = new BufferedReader(new FileReader(savePath));
					String text = null;
					while ((text = reader.readLine()) != null){
						 buf.append(text);
						 buf.append(System.getProperty("line.separator"));
					}
					courseAssessment = buf.toString();
				} catch (IOException e) {
					log.info("read outline failed : ",e);
				} finally{
					try {
						if(reader != null)
							reader.close();
					} catch (IOException e) {
					}
				} 
			}
		}
		return courseAssessment;
	}

	public void setCourseAssessment(String courseAssessment) {
		this.courseAssessment = courseAssessment;
	}

	public boolean isShowEditOutline() {
		log.info("--------isShowEditOutline()---showEditOutline = "+showEditOutline);
		return showEditOutline;
	}

	public void setShowEditOutline(boolean showEditOutline) {
		this.showEditOutline = showEditOutline;
	}

	public String getEvaluationFileName() {
		return evaluationFileName;
	}

	public void setEvaluationFileName(String evaluationFileName) {
		this.evaluationFileName = evaluationFileName;
	}

	public boolean isShowEditEvaluation() {
		return showEditEvaluation;
	}

	public void setShowEditEvaluation(boolean showEditEvaluation) {
		this.showEditEvaluation = showEditEvaluation;
	}
}
